package com.boshao.classschedule.fragment;

import java.util.List;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SearchView;
import android.widget.TextView;

import com.boshao.classschedule.R;
import com.boshao.classschedule.activity.MainActivity;
import com.boshao.classschedule.model.LifeSchedule;
import com.boshao.classschedule.model.LifeScheduleDao;
import com.boshao.classschedule.model.LifeScheduleDao.Properties;
import com.boshao.classschedule.utils.ViewHolder;
import com.boshao.classschedule.widget.ActionBarLeft;
import com.boshao.classschedule.widget.ActionBarRight;

import de.greenrobot.dao.query.QueryBuilder;

/**
 * 搜索界面的Fragment
 * @author boshao
 *
 */
public class SearchFragment extends AbstartFragment implements 
SearchView.OnQueryTextListener,OnItemClickListener{
	private static final String TAG="search_fragment";
	private View mRootView;
	private SearchView mSearchView;
	private ListView mListView;
	public static SearchFragment newInstance(){
		SearchFragment fragment = new SearchFragment();
		return fragment;
	}
	
	@Override
	protected String getFragmentTag() {
		return TAG;
	}
	
	@Override
	protected int getContentLayoutId() {
		return R.id.rl_search;
	}
	
	@Override
	protected ActionBarLeft getActionBarLeft() {
		return ActionBarLeft.BACK;
	}
	
	@Override
	protected void onClickActionLeft() {
		onBackPressed();
	}
		
	@Override
	protected ActionBarRight getActionBarRight() {
		return ActionBarRight.NULL;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = super.onCreateView(inflater, container, savedInstanceState);
		RelativeLayout rlActionbar = (RelativeLayout) view.findViewById(R.id.rl_actionbar);
		mSearchView = new SearchView(getActivity());
		mSearchView.setIconifiedByDefault(false);
		mSearchView.setOnQueryTextListener(this);
		rlActionbar.addView(mSearchView);
		RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mSearchView.getLayoutParams();
		layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
		layoutParams.height = RelativeLayout.LayoutParams.MATCH_PARENT;
		layoutParams.addRule(RelativeLayout.RIGHT_OF,R.id.actionbar_left_back);		
		return view;
	}
	
	@Override
	protected View onAppCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		mRootView = inflater.inflate(R.layout.fragment_search,container,false);
		mListView = (ListView) mRootView.findViewById(R.id.lv_search_result);
		mListView.setOnItemClickListener(this);
		return mRootView;
	}

	@Override
	public boolean onQueryTextSubmit(String query) {
		query(query);
		return true;
	}

	private void query(String query) {
		String q = "%"+query+"%";
		LifeScheduleDao dao = readLifeScheduleDao();
		QueryBuilder<LifeSchedule> qb = dao.queryBuilder();
		qb.where(qb.or(Properties.Activity_name.like(q),
				Properties.Address.like(q),
				Properties.Description.like(q)));
		List<LifeSchedule> lifeSchedules = qb.list();
		mListView.setAdapter(new SearchAdapter<>(lifeSchedules));
	}

	@Override
	public boolean onQueryTextChange(String newText) {
		
		return false;
	}

	@Override
	protected boolean onBackPressed() {
		hideSoftInput(mSearchView.getWindowToken());
		return super.onBackPressed();
	}
	
	class SearchAdapter<T> extends BaseAdapter{
		private List<T> t;
		private int count;
		public SearchAdapter(List<T> t) {
			this.t = t;
			if(t == null || t.isEmpty()){
				count = 0;
			}else{
				count = t.size();
			}
		}

		public List<T> getLifeSchedules(){
			return t;
		}
		
		@Override
		public int getCount() {
			return count;
		}

		@Override
		public Object getItem(int position) {
			return t == null || t.isEmpty() ? null : t.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null){
				convertView = LayoutInflater.from(parent.getContext()).inflate
						(R.layout.listview_item_search,parent,false);
			}
			TextView tvName = ViewHolder.get(convertView,R.id.tv_name);
			if(t != null && !t.isEmpty()){
				if(t.get(position) instanceof LifeSchedule){
					LifeSchedule lifeSchedule = (LifeSchedule) t.get(position);
					tvName.setText(String.format("日程名称: %s",lifeSchedule.getActivity_name()));					
				}
			}
			return convertView;
		}
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		hideSoftInput(view.getWindowToken());
		List<LifeSchedule> lifeSchedules = ((SearchAdapter<LifeSchedule>)parent.getAdapter()).getLifeSchedules();
		TimeManagerFragment fragment = TimeManagerFragment.newInstance(null,lifeSchedules.get(position));
		((MainActivity)getActivity()).addFragment(getActivity().getSupportFragmentManager(), fragment,fragment.getFragmentTag(),true);
	}
}
